<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class MobileBasketService extends ServiceBase {


    public function getList($inputs = array()){
        //允许排序的字段名称数组
        $canOrderCols = array('id', 'count', 'updated_at', 'created_at');

        $order = $this->getSearchField($inputs, 'order');
        $reverse = $this->getSearchField($inputs, 'reverse', 0 );
        $page = $this->getSearchField($inputs, 'page', 1 );

        $member_id = $this->getSearchField($inputs,  'member_id', null );
        $food_id = $this->getSearchField($inputs,  'food_id', null );
        $count = $this->getSearchField($inputs,  'count', null );
        $created_at_start = $this->getSearchField($inputs,  'created_at_end', null );
        $created_at_end = $this->getSearchField($inputs,  'created_at_end', null );
        $updated_at_start = $this->getSearchField($inputs,  'updated_at_start', null );
        $updated_at_end = $this->getSearchField($inputs,  'updated_at_end', null );

        $entries = Basket::orderby(in_array($order, $canOrderCols)?$order:'updated_at', $reverse == 0?'desc':'asc');

        if(!empty($created_at_start)){
            $created_at_start = strtotime($created_at_start);
            $created_at_start = date('Y-m-d', $created_at_start).' 00:00:00';

            $entries = $entries->where('created_at', '>=', $created_at_start);
        }

        if(!empty($created_at_end)){
            $created_at_end = strtotime($created_at_end);
            $created_at_end = date('Y-m-d', $created_at_end).' 23:59:59';

            $entries = $entries->where('created_at', '<=', $created_at_end);
        }

        if(!empty($updated_at_start)){
            $updated_at_start = strtotime($updated_at_start);
            $updated_at_start = date('Y-m-d', $updated_at_start).' 00:00:00';

            $entries = $entries->where('updated_at', '>=', $updated_at_start);
        }

        if(!empty($updated_at_end)){
            $updated_at_end = strtotime($updated_at_end);
            $updated_at_end = date('Y-m-d', $updated_at_end).' 23:59:59';

            $entries = $entries->where('updated_at', '<=', $updated_at_end);
        }

        if(!empty($member_id)){
            $entries = $entries->where('member_id', $member_id);
        }

        if(!empty($food_id)){
            $entries = $entries->where('food_id', $food_id);
        }

        if(!empty($count)){
            $entries = $entries->where('count', $count);
        }

        $entries = $entries->get();

        $links = array(
            "order"=>$order,
            "reverse"=>$reverse,
            "page"=>$page,
            'created_at_start' => $created_at_start,
            'created_at_end' => $created_at_end,
            'updated_at_start' => $updated_at_start,
            'updated_at_end' => $updated_at_end,
            'member_id' => $member_id,
            'food_id' => $food_id,
            'count' => $count,
        );

        return array(
            'entries'=>$entries,
            'links'=>$links,
        );

    }

    public function addFood($member_id, $food_id, $count = 1){
        if(Basket::where('member_id', $member_id)->select('id')->count()>=20){
            $this->errors()->add('error', '购物车中限制只能添加20种餐品，下单后清除购物车。');
            return false;
        }
        $basket = Basket::where('member_id', $member_id)->where('food_id', $food_id)->first();
        if(!$basket){
            $basket = new Basket();
            $basket->member_id =$member_id;
            $basket->food_id =$food_id;
            $basket->count =$count;
        }else{
            $basket->count = $count;
        }

        if(!$basket->save()){
            $this->errors()->merge($basket->errors());
            return false;
        }

        return true;
    }

    public function dropFood($member_id, $food_id){
        $basket = Basket::where('member_id', $member_id)->where('food_id', $food_id)->first();

        if(!$basket->delete()){
            $this->errors()->merge($basket->errors());
            return false;
        }
        return true;
    }

    public function deleteBasket($basket){
        if(!$basket->delete()){
            $this->errors()->merge($basket->errors());
            return false;
        }
        return true;
    }

    public function exportBasket(){
        $result = $this->getList();
        $table = $result['entries'];
        $type = Input::get( 'type' );

        $columnTitle = Basket::$column;

        switch($type){
            case 'csv':
                $output='';

                $output .=  implode(",",$columnTitle)."\r\n";

                $output .= Basket::getCSVContent($table);

                $headers = array(
                    'Content-Type' => "text/csv;charset='utf-8'",
                    'Content-Disposition' => 'attachment; filename="basket_'.date('Y_m_d_His').'.csv"',
                );

                return array('output'=>rtrim($output, "\r\n"), 'headers'=>$headers);
            case 'excel':

                $output = '<table><thead><tr>';
                foreach($columnTitle as $columnName){
                    $output = $output.'<th>'.$columnName.'</th>';
                }
                $output = $output.'</tr></thead>';

                $output = $output.Basket::getExcelContent($table);

                $output = $output.'<tfoot><tr><td colspan="'.count($columnTitle).'"></td></tr></tfoot></table>';

                $headers = array(
                    'Pragma' => 'public',
                    'Expires' => 'public',
                    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
                    'Cache-Control' => 'private',
                    'Content-Type' => "application/vnd.ms-excel;;charset='utf-8'",
                    'Content-Disposition' => 'attachment; filename=basket_'.date('Y_m_d_His').'.xls',
                    'Content-Transfer-Encoding' => ' binary'
                );
                return array('output'=>$output, 'headers'=>$headers);
        }
        $this->errors()->add('error', '导出失败');
        return false;
    }


    private function getSearchField($inputs, $key, $default = null){
        return isset($inputs[$key]) ? $inputs[$key] : Input::get($key, $default);
    }

}

